#include <vector>
#include <iostream>
#include <cassert>
using namespace std;

// 手撕最大堆
// 提供：插入、删除元素
// 成员函数：向下调整、向上调整
class maxHeap
{
public:
    maxHeap(): size(0){}
    void Push(int item)
    {
        heap.push_back(item);
        adjustUp(size);
        size++;
    }
    int Pop()
    {
        assert(size > 0);
        int top = heap[0];
        swap(heap[0], heap[size - 1]);
        heap.pop_back();
        size--;
        adjustDown(0);
        return top;
    }
    void Print()
    {
        for(int item : heap)
        {
            cout << item << " ";
        }
        cout << endl;
    }
    bool Empty()
    {
        return size == 0;
    }
private:
    void adjustUp(int child)
    {
        int parent = (child - 1) / 2;
        while(child > 0)
        {
            if(heap[child] > heap[parent])
            {
                swap(heap[child], heap[parent]);
                child = parent;
                parent = (child - 1) / 2;
            }
            else break;
        }
    }
    void adjustDown(int parent)
    {
        int child = 2 * parent + 1;
        while(child < size)
        {
            if(child + 1 < size && heap[child + 1] > heap[child])
            {
                child++;
            }
            if(heap[child] > heap[parent])
            {
                swap(heap[child], heap[parent]);
                parent = child;
                child = 2 * parent + 1;
            }
            else break;
        }

    }
private:
    vector<int> heap;
    int size;
};